package com.hzdl.web.controller;

import com.hzdl.web.bean.User;
import com.hzdl.web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Sfx
 * Date: 2018/9/3 0003
 * Time: 上午 7:14
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(String account, String password, HttpSession session) {
        User user = userService.login(account, password);
        session.setAttribute("user",user);
        System.out.println(user);
        return "forward:/user/getAll";
    }
    @RequestMapping("/addUser")
    public String upload(MultipartFile file, User user) throws IOException {


        String path = "F:\\upload\\";
        File file1 = new File(path+file.getOriginalFilename());
        //把上传的文件转存储到制定路径
        file.transferTo(file1);
        String photo = file.getOriginalFilename();
        user.setPhoto(photo);
        userService.addUser(user);
        return "forward:/user/getAll";
    }
    @RequestMapping("/getAll")
    public String getAll(Model model){
        List<User> users=userService.getAll();
        model.addAttribute("users",users);
        return "userDelete";
    }
    @RequestMapping("/toRegist")
    public String toRegist(){
        return "regist";
    }

    @RequestMapping("/toLogin")
    public String toLogin(){
        return "index";
    }


}
